var { pool } = require('../utils/dbUtil')

// 查找admin
function selectAllGoods(obj) {
    let { aTypeId, bTypeId, goodName, page, num } = obj
    // let sql = "SELECT * FROM good WHERE goodName LIKE '%?%' and atypeId LIKE '%?%' and bTypeId LIKE '%?%' limit (page-1)*num,num";
    // let sql = `SELECT atype.typeName 'aTypeName',btype.typeName 'bTypeName',good.* FROM good,atype,btype WHERE atype.aTypeId=good.aTypeId and btype.bTypeId=good.bTypeId and goodName LIKE concat('%',?,'%') and good.atypeId LIKE concat('%',?,'%') and good.bTypeId LIKE concat('%',?,'%') limit ${(page-1)*num},${num}`;
    // let sql = `SELECT * FROM good WHERE goodName LIKE concat('%',?,'%') and atypeId LIKE concat('%',?,'%') and bTypeId LIKE concat('%',?,'%') limit ${(page-1)*num},${num}`;
    // let sql = `SELECT atype.typeName 'aTypeName',btype.typeName 'bTypeName',good.* FROM atype RIGHT JOIN btype ON atype.aTypeId = btype.aTypeId RIGHT JOIN good ON btype.bTypeId = good.bTypeId and btype.aTypeId = good.aTypeId and goodName LIKE concat('%',?,'%') and good.atypeId LIKE concat('%',?,'%') and good.bTypeId LIKE '%?%' limit ${(page - 1) * num},${num}`
    let sql = `SELECT atype.typeName 'aTypeName',btype.typeName 'bTypeName',good.* FROM atype RIGHT JOIN btype ON atype.aTypeId = btype.aTypeId RIGHT JOIN good ON btype.bTypeId = good.bTypeId and btype.aTypeId = good.aTypeId and goodName LIKE concat('%',?,'%') and good.aTypeId LIKE concat('%',?,'%') and good.bTypeId LIKE concat('%',?,'%') order by goodOrder,goodTime LIMIT ${(page - 1) * num},${num}`
    return new Promise((resolve, reject) => {
        pool.getConnection(function (connError, connection) {
            if (connError) {
                reject(connError)
            } else {
                connection.query(sql, [goodName, aTypeId, bTypeId, page, num], (queryError, rows) => {
                    if (queryError) {
                        reject(queryError)
                    } else {
                        resolve(rows)
                    }
                })
            }
        })
    })
}

module.exports = {
    selectAllGoods
}